from django.shortcuts import render
from .models import Article,Category
from django.http import HttpResponse
from datetime import datetime,time
from django.utils.timezone import make_aware
# Create your views here.
def index(request):
    #exact 解析成= iexact 解析成like
    #like 和 = 大部分情况下等价  少部分不等价
    # article = Article.objects.filter(title__exact="种花")
    article = Article.objects.filter(title__iexact="种花")
    print(article.query)
    print(article)

    return HttpResponse("success")

#query 用来查看 orm被最终翻译成的 sql语句  但是只能用在Queryset 对象上  而不能用在普通的ORM模型上
#如果你的查询语句是通过get来获取数据的 那么就不能使用query  因为get返回的不是Queryset
#如果通过filter 或者其它返回QuerySet 可以使用我们的query
def index1(request):
    article = Article.objects.get(pk=3)
    print(article) #这是普通的ORM模型  query不能用
    return HttpResponse("index1")

def index2(request):
    # article = Article.objects.filter(title__contains="种花")
    article = Article.objects.filter(title__icontains="种花")
    print(article.query)
    print(article)
    return HttpResponse("index2")

def index3(request):
    # articles = Article.objects.filter(id__in=[1,3,5])
    # print(articles.query)
    # for article in articles:
    #     print(article)

    # categories = Category.objects.filter(articles__in=[1,2,4])
    # for category in  categories:
    #     print(category)
    # return HttpResponse("index3")

    #所有标题种包含kangbazi的
    articles = Article.objects.filter(title__icontains='kangbazi')
    categories = Category.objects.filter(articles__in=articles)
    for category in categories:
        print(category)
    print(categories.query)

    return HttpResponse("index3")


def index4(request):
    # gt  greeter than  大于
    #lt   lower than
    #gte  greeter than equal
    article = Article.objects.filter(id__lte=3)
    print(article.query)
    print(article)
    return HttpResponse("index4")

def index5(request):
    # istartswith 以什么开头  不区分大小写
    #startswith 以什么开头  严格区分大小写
    # article = Article.objects.filter(title__startswith="i")
    # article = Article.objects.filter(title__istartswith="i")
    article = Article.objects.filter(title__endswith="zi")
    print(article.query)
    print(article)
    return HttpResponse("index5")


def index6(request):
    start_time = make_aware(datetime(year=2019,month=4,day=6,hour=10,minute=0,second=0))
    end_time = make_aware(datetime(year=2019,month=6,day=6,hour=16,minute=0,second=0))
    article = Article.objects.filter(create_time__range=(start_time,end_time))
    print(article.query)
    print(article)
    return HttpResponse("index6")

def index7(request):
    # start_time = make_aware(datetime(year=2019,month=4,day=6,hour=10,minute=0,second=0))
    # end_time = make_aware(datetime(year=2019,month=6,day=6,hour=16,minute=0,second=0))
    # article = Article.objects.filter(create_time__date=datetime(year=2019,month=5,day=14))
    #article = Article.objects.filter(create_time__date=datetime(year=2019,month=5,day=14))
    #article = Article.objects.filter(create_time__year__gte=2018)
    # article = Article.objects.filter(create_time__week_day=4)
    start_time = time(hour=14,minute=30,second=0)
    end_time = time(hour=15,minute=40,second=0)
    article = Article.objects.filter(create_time__time__range=(start_time,end_time))
    print(article.query)
    print(article)
    return HttpResponse("index7")

def index8(request):
    article = Article.objects.filter(create_time__isnull=False)
    print(article.query)
    print(article)
    return HttpResponse("index8")

def index9(request):
    article = Article.objects.filter(title__iregex=r'^mysql')
    print(article.query)
    print(article)
    return HttpResponse("index9")

def index10(request):
    categories = Category.objects.filter(articles__title__contains="mysql")
    print(categories.query)
    print(categories)
    return HttpResponse("index10")